﻿/*************************************************************************
 *  Copyright (C), 2017-2018, YangTao tech. Co., Ltd.
 *  FileName: ClientManage.cs
 *  Author: YangTao   Version: 1.0   Date: 11/28/2016
 *  Email:YangTao_Unity3D@163.com
 *  Version Description:
 *    
 *  File Description:
 *    
 *  Class List:
 *    <ID>           <name>             <description>
 *     1.         ClientManage             
 *  Function List:
 *    <ID>           <name>             <description>
 *     1.
 *  History:
 *    <ID>    <author>      <time>      <version>      <description>
 *     1.     YangTao     11/28/2016       1.0        Build this file.
 *************************************************************************/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Net;
using System.Linq;

/// <summary>客户端管理</summary>
public class ClientManage
{
    /// <summary>存储客户端字典</summary>
    Dictionary<string, Socket> m_clientDic;//存放客户端IP端口与对应Socket的字典 
    /// <summary>存储客户端心跳字典</summary>
    Dictionary<string, bool> m_clientHeartDic;
    public Void_StringDelegate AddNewClientCallback;

    public ClientManage()
    {
        m_clientDic = new Dictionary<string, Socket>();
        m_clientHeartDic = new Dictionary<string, bool>();
    }

    /// <summary>增加客戶端</summary>
    public void AddClient(Socket s)
    {
        string ipPort = s.RemoteEndPoint.ToString();
        if (!m_clientDic.ContainsKey(ipPort))
        {
            m_clientDic.Add(ipPort, s);
            m_clientHeartDic.Add(ipPort, true);
            AddNewClientCallback(ipPort);
        }
    }

    /// <summary>刪除客戶端</summary>
    public void DeleteClient(string ipPort)
    {
        if (m_clientDic.ContainsKey(ipPort))
            m_clientDic.Remove(ipPort);
        if (m_clientHeartDic.ContainsKey(ipPort))
            m_clientHeartDic.Remove(ipPort);
    }

    public void DeleteClient(Socket s)
    {
        string ipPort = s.RemoteEndPoint.ToString();
        DeleteClient(ipPort);
    }

    /// <summary>查找客戶端</summary>
    public Socket FindClient(string ipPort)
    {
        if (m_clientDic.ContainsKey(ipPort))
            return m_clientDic[ipPort];
        else
            return null;
    }

    /// <summary>查找客戶端心跳状态</summary>
    public bool FindClientHeart(string ipPort)
    {
        if (m_clientHeartDic.ContainsKey(ipPort))
            return m_clientHeartDic[ipPort];
        else
            return false;
    }

    /// <summary>改变客戶端心跳状态</summary>
    public void ChangeClientHeart(string ipPort)
    {
        if (m_clientHeartDic.ContainsKey(ipPort))
            m_clientHeartDic[ipPort] = true;
    }

    /// <summary>查找已离线客戶端</summary>
    public List<string> FindLeaveClient()
    {
        List<string> list = new List<string>();
        //var e = m_clientHeartDic.GetEnumerator();
        //foreach(var key in m_clientHeartDic.Keys)
        //{
        //    var value = true;
        //    m_clientHeartDic.TryGetValue(key, out value);
        //    if (value)
        //    {
        //        m_clientHeartDic[key] = false;
        //    }
        //    else
        //        list.Add(key);
        //}
        
        string[] Array = m_clientHeartDic.Keys.ToArray<string>();
        foreach (string str in Array)
        {
            if (m_clientHeartDic[str])
            {
                m_clientHeartDic[str] = false;
            }
            else
            {
                list.Add(str);
                m_clientHeartDic.Remove(str);
            }            
        }
        //while (e.MoveNext())
        //{
        //    string ip = e.Key.ToString();
        //    if (m_clientHeartDic[ip])
        //    {              
        //        m_clientHeartDic[ip] = false;
        //    }
        //    else
        //        list.Add(ip);
        //    //m_clientHeartDic.Remove(ip);
        //}

        if (list.Count == 0)
            list = null;

        return list;
    }

    /// <summary>
    /// 获得所有客户端
    /// </summary>
    /// <returns>所有客户端链表</returns>
    public List<string> GetAllClientIPPort()
    {
        List<string> list = new List<string>();
        IDictionaryEnumerator e = m_clientDic.GetEnumerator();
        while (e.MoveNext())
        {
            list.Add((string)e.Key);
        }

        if (list.Count == 0)
            list = null;

        return list;
    }

    /// <summary>关闭所有Socket</summary>
    public void CloseAllSocket()
    {
        IDictionaryEnumerator e = m_clientDic.GetEnumerator();
        while(e.MoveNext())
        {
            Socket s = (Socket)e.Value;
            if (s != null)
                s.Close();
        }

        m_clientDic.Clear();
    }
}
